package richinfo.delivery.service;

import richinfo.delivery.bean.MemcachedInfo;

import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;

public class MemcachedService {

	private static MemcachedService memcachedService = new MemcachedService();

	private MemCachedClient memCachedClient = null;

	private SockIOPool sockIOPool = null;

	private MemcachedInfo memcachedInfo;

	private MemcachedService() {		
		this.memCachedClient = new MemCachedClient();
		this.sockIOPool = SockIOPool.getInstance();}

	public static MemcachedService getInstance() {
		return memcachedService;
	}

	public MemcachedInfo getMemcachedInfo() {
		return memcachedInfo;
	}

	public void init(MemcachedInfo memcachedInfo) {
		this.memcachedInfo = memcachedInfo;
		this.memCachedClient.setPrimitiveAsString(memcachedInfo
				.isPrimitiveAsString());
		initPool();
	}

	private void initPool() {
		sockIOPool.setServers(memcachedInfo.getServers());
		sockIOPool.setWeights(memcachedInfo.getWeights());
		sockIOPool.setInitConn(memcachedInfo.getInitConn());
		sockIOPool.setMinConn(memcachedInfo.getMinConn());
		sockIOPool.setMaxConn(memcachedInfo.getMaxConn());
		sockIOPool.setMaintSleep(memcachedInfo.getMaintSleep());
		sockIOPool.setNagle(memcachedInfo.isNagle());
		sockIOPool.setSocketTO(memcachedInfo.getSocketConnTimeOut());
		sockIOPool.setSocketConnectTO(memcachedInfo.getSocketConnTimeOut());
	}

	public void connect() {
		sockIOPool.initialize();
	}

	public void close() {
		sockIOPool.shutDown();
	}

	public Object get(String key) {
		return memCachedClient.get(key);
	}

	/**add mem if exist return false else true
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean add(String key, Object value) {
		return memCachedClient.add(key, value);
	}

	public boolean add(String key, Object value, Integer expire) {
		return memCachedClient.add(key, value, expire);
	}

	/**set mem whether if exist return true
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(String key, Object value) {
		return memCachedClient.set(key, value);
	}

	public boolean set(String key, Object value, Integer expire) {
		return memCachedClient.set(key, value, expire);
	}

	public boolean replace(String key, Object value) {
		return memCachedClient.replace(key, value);
	}

	public boolean replace(String key, Object value, Integer expire) {
		return memCachedClient.replace(key, value, expire);
	}

	public boolean delete(String key) {
		return memCachedClient.delete(key);
	}

	public boolean flushAll(String[] keys) {
		return memCachedClient.flushAll(keys);
	}

	public synchronized boolean synAdd(String key, Object value) {
		return memCachedClient.add(key, value);
	}

	public synchronized boolean synDel(String key) {
		return memCachedClient.delete(key);
	}

	public synchronized boolean synReplace(String key, String value) {
		return memCachedClient.replace(key, value);
	}

	public synchronized boolean synSet(String key, String value) {
		return memCachedClient.set(key, value);
	}

	public synchronized boolean synClear() {
		return memCachedClient.flushAll();
	}

}
